package gpl.scotlandyard.ui.tools;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;

/**
 * A layered layout manager.
 * @author Norbert Martin
 */
public class LayeredLayout implements LayoutManager {

  @Override
  public void addLayoutComponent(String name, Component comp) {
    // nothing
  }

  @Override
  public void removeLayoutComponent(Component comp) {
    // nothing
  }

  @Override
  public Dimension preferredLayoutSize(Container parent) {
    Component[] components = parent.getComponents();
    if (components.length == 0) {
      return new Dimension(0, 0);
    }
    Dimension d = components[0].getPreferredSize();
    for (int i = 1; i < components.length; i++) {
      Dimension tmp = components[i].getPreferredSize();
      if (d.width < tmp.width) {
        d.width = tmp.width;
      }
      if (d.height < tmp.height) {
        d.height = tmp.height;
      }
    }
    return d;
  }

  @Override
  public Dimension minimumLayoutSize(Container parent) {
    Component[] components = parent.getComponents();
    if (components.length == 0) {
      return new Dimension(0, 0);
    }
    Dimension d = components[0].getMinimumSize();
    for (int i = 1; i < components.length; i++) {
      Dimension tmp = components[i].getMinimumSize();
      if (d.width < tmp.width) {
        d.width = tmp.width;
      }
      if (d.height < tmp.height) {
        d.height = tmp.height;
      }
    }
    return d;
  }

  @Override
  public void layoutContainer(Container parent) {
    for (Component c : parent.getComponents()) {
      c.setSize(c.getPreferredSize());
    }
  }
}
